﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using knfUpdateDataLayer;

namespace LibreriaNuovaExcel
{
    public class collegamentoDb
    {
        private knfEntities _db = new knfEntities();

        public List<localSpostamentoDati> ListaDati { get; set; }


        private localSpostamentoDati EstraiPeriodo(List<RawData> InListReporter, List<RawData> InListPartner, double Period)
        {
            try
            {


                var ValoriMesi = from q in InListReporter where q.PERIOD.Value == Period select q;

                decimal tonn = 0;
                decimal euro = 0;

                if (ValoriMesi.Count() > 0)
                {
                    decimal tonnA = 0;
                    decimal euroA = 0;

                    foreach (RawData tmpRawData in ValoriMesi)
                    {
                        if (tmpRawData.INDICATORS_LAB == "VALUE_IN_EUROS")
                        {
                            euroA += (decimal)tmpRawData.INDICATOR_VALUE.Value;
                        }
                        else if (tmpRawData.INDICATORS_LAB == "QUANTITY_IN_100KG")
                        {
                            tonnA += (decimal)tmpRawData.INDICATOR_VALUE.Value / 10;
                        }

                    }

                    tonn = tonnA;

                    if (tonnA != 0)
                    {
                        euro = euroA / tonnA;
                    }
                }
                else
                {
                    var ValoriMesiPartner = from q in InListPartner where q.PERIOD == Period select q;

                    if (ValoriMesiPartner.Count() > 0)
                    {
                        decimal tonnA = 0;
                        decimal euroA = 0;

                        foreach (RawData tmpRawData in ValoriMesiPartner)
                        {
                            if (tmpRawData.INDICATORS_LAB == "VALUE_IN_EUROS")
                            {
                                euroA += (decimal)tmpRawData.INDICATOR_VALUE.Value;
                            }
                            else if (tmpRawData.INDICATORS_LAB == "QUANTITY_IN_100KG")
                            {
                                tonnA += (decimal)tmpRawData.INDICATOR_VALUE.Value / 10;
                            }
                        }

                        tonn = tonnA;

                        if (tonnA != 0)
                        {
                            euro = euroA / tonnA;
                        }
                    }
                }

                localSpostamentoDati ldb = new localSpostamentoDati(Period, Math.Round(tonn, 2), Math.Round(euro));

                return ldb;
            }
            catch
            {
                decimal tonnB = 0;
                decimal euroB = 0;

                localSpostamentoDati ldb = new localSpostamentoDati(Period, tonnB, euroB);
                return ldb;

            }


        }



        private decimal DammiMedia(decimal Uno, decimal Due)
        {
            if (Uno != 0 && Due != 0)
            {
                return (Uno + Due) / 2;
            }
            else
            {
                return (Uno + Due);
            }

        }



        private localSpostamentoDati EstraiPeriodoMediato(List<RawData> InListReporter, List<RawData> InListPartner, double Period)
        {
            try
            {
                var ValoriMesi = from q in InListReporter where q.PERIOD.Value == Period select q;

                decimal tonnUno = 0;
                decimal euroUno = 0;
                decimal tonnDue = 0;
                decimal euroDue = 0;

                decimal tonnMediato = 0;
                decimal euroMediato = 0;



                if (ValoriMesi.Count() > 0)
                {
                    decimal tonnAUno = 0;
                    decimal euroAUno = 0;

                    foreach (RawData tmpRawData in ValoriMesi)
                    {
                        if (tmpRawData.INDICATORS_LAB == "VALUE_IN_EUROS")
                        {
                            euroAUno += (decimal)tmpRawData.INDICATOR_VALUE.Value;
                        }
                        else if (tmpRawData.INDICATORS_LAB == "QUANTITY_IN_100KG")
                        {
                            tonnAUno += (decimal)tmpRawData.INDICATOR_VALUE.Value / 10;
                        }

                    }

                    tonnUno = tonnAUno;

                    if (tonnAUno != 0)
                    {
                        euroUno = euroAUno / tonnAUno;
                    }
                }


                var ValoriMesiDue = from q in InListPartner where q.PERIOD.Value == Period select q;
                if (ValoriMesiDue.Count() > 0)
                {
                    decimal tonnADue = 0;
                    decimal euroADue = 0;

                    foreach (RawData tmpRawData in ValoriMesiDue)
                    {
                        if (tmpRawData.INDICATORS_LAB == "VALUE_IN_EUROS")
                        {
                            euroADue += (decimal)tmpRawData.INDICATOR_VALUE.Value;
                        }
                        else if (tmpRawData.INDICATORS_LAB == "QUANTITY_IN_100KG")
                        {
                            tonnADue += (decimal)tmpRawData.INDICATOR_VALUE.Value / 10;
                        }

                    }

                    tonnDue = tonnADue;

                    if (tonnADue != 0)
                    {
                        euroDue = euroADue / tonnADue;
                    }
                }

                tonnMediato = DammiMedia(tonnUno, tonnDue);
                euroMediato = DammiMedia(euroUno, euroDue);




                localSpostamentoDati ldb = new localSpostamentoDati(Period, Math.Round(tonnMediato, 2), Math.Round(euroMediato));

                return ldb;
            }
            catch 
            {
                decimal tonnB = 0;
                decimal euroB = 0;

                localSpostamentoDati ldb = new localSpostamentoDati(Period, tonnB, euroB);
                return ldb;            
            }


        }






        public collegamentoDb(string reporter, string partner, string flow, int anno, bool averaged, 
                List<RawData> ReporterList, List<RawData>PartnerList)
        {


            List<localSpostamentoDati> listaLocale = new List<localSpostamentoDati>();

            for (int i = 1; i <= 12; i++) {               
               
                double Periodo = anno * 100 + i;


                if (averaged == false)
                {
                    localSpostamentoDati gDato = EstraiPeriodo(ReporterList, PartnerList, Periodo);
                    listaLocale.Add(gDato);
                }
                else
                {
                    localSpostamentoDati gDatoMediato = EstraiPeriodoMediato(ReporterList, PartnerList, Periodo);
                    listaLocale.Add(gDatoMediato);                
                }
               
            }
            this.ListaDati = listaLocale;
	    }
    }
}
